package com.zeropol2.anyquestion.app.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zeropol2.anyquestion.R;
import com.zeropol2.anyquestion.component.ui.list.ArticleViewList;
import com.zeropol2.anyquestion.component.ui.listitem.ArticleView;
import com.zeropol2.anyquestion.component.ui.widget.PageIndicatorView;

/**
 * @author zeropol2 조회탭 프래그먼트
 */
public class ViewFragment extends Fragment {
	private Context mContext;
	private List<ArticleView> mList;
	private ArticleView mArticleView;
	private ArticleViewList mArticleViewList;
	private PageIndicatorView mPageIndicator;
	private PageIndicatorView.PageIndicatorClickListener l = new PageIndicatorView.PageIndicatorClickListener() {

		@Override
		public void onGoPrevClick() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGoNextClick() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGoLastClick() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGoFirstClick() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGoCountClick(int location) {
			loadArticleList(location,
					PageIndicatorView.ARTICLE_COUNT_PER_PAGE);
		}
	};

	public ViewFragment() {
		super();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mContext = activity.getApplicationContext();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		// FIXME 데이터 로더로 대체해야함
		mList = new ArrayList<ArticleView>();
		for (int i = 0; i < 90; i++) {
			mArticleView = new ArticleView(mContext);
			mArticleView.setTitle("질문받습니다" + i);
			mArticleView.setDate(System.currentTimeMillis());
			mArticleView.setReplyCount(i);
			mList.add(mArticleView);
		}

		View v = inflater.inflate(R.layout.fragment_view, container, false);
		mArticleViewList = (ArticleViewList) v.findViewById(R.id.view_list);
		mPageIndicator = (PageIndicatorView) v
				.findViewById(R.id.view_page_indicator);
		loadArticleList(1, PageIndicatorView.ARTICLE_COUNT_PER_PAGE);
		mPageIndicator
				.setPageIndicatorClickListener(l);
		l.onGoCountClick(0);

		return v;
	}

	public void loadArticleList(int from, int count) {
		
		int realFrom = count * from;
		int realTo = realFrom + count;
		List<ArticleView> mList2 = new ArrayList<ArticleView>();
		try {
			for (int i = realFrom; i < realTo; i++) {
				mList2.add(mList.get(i));
			}
		} catch (IndexOutOfBoundsException e) {

		} finally {
			mArticleViewList.setListItem(mList2);
			mPageIndicator.setTotalPageCount(mList.size());
		}
	}

}
